package cn.schoolwow.util.test.module.query.instanceList.flow.query;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.test.module.common.domain.User;
import org.junit.Assert;

import java.util.List;

public class InstanceQueryOrderByFieldNameFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        List<User> userList = (List<User>) flowContext.checkData("userList");

        {
            List<User> filterList = QueryInstanceList.newQuery(userList)
                    .orderByAsc("age")
                    .execute()
                    .getList();
            Assert.assertEquals(userList.size(), filterList.size());
            for(int i=1;i<filterList.size();i++){
                int age = filterList.get(i).age.intValue();
                Assert.assertEquals(i, age);
            }
            Assert.assertNull(filterList.get(0).age);
        }
        {
            List<User> filterList = QueryInstanceList.newQuery(userList)
                    .orderByDesc("age")
                    .execute()
                    .getList();
            Assert.assertEquals(userList.size(), filterList.size());
            for(int i=0;i<filterList.size()-1;i++){
                int age = filterList.get(i).age.intValue();
                Assert.assertEquals(filterList.size()-1-i, age);
            }
            Assert.assertNull(filterList.get(filterList.size()-1).age);
        }
    }

    @Override
    public String name() {
        return "用户实例列表按照年龄排序";
    }
}
